<template>
	<view class="content">
		<h2 style='color:#007aff;margin-top: 30px;'>欢 迎 来 到 聊 天 室</h2>
		<uni-forms errShowType="modal" style="width: 100%;margin-top:50px;height: 350px;display: flex;flex-direction: column;padding: 30px 30px;" :model="formData" :rules="rules" ref="form">
			<uni-forms-item  label="用户名" name="userName">
				<uni-easyinput required v-model="formData.userName"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item  style="margin:70px 0;" label="密码" name="upwd">
				<uni-easyinput required type="password" v-model="formData.upwd"></uni-easyinput>
			</uni-forms-item>
			<button type="primary" @click="login">登录</button>
		</uni-forms>
		
	</view>
</template>

<script>
	import {userLogin,queryByName} from '../../service/index.js'
	export default {
		data() {
			return {
				formData:{
					userName:'',
					upwd:'',
				},
				rules:{
					userName:{
						rules:[
							{required:true,errorMessage:'用户名不能为空'}
						]
					},
					upwd:{
						rules:[
							{required:true,errorMessage:'密码不能为空'}
						]
					}
				}
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			login(form){
				this.$refs.form.validate().then(async res=>{
					//console.log(res)
					let data=await userLogin(this.formData.userName,this.formData.upwd)
					//console.log(data)
					if(data.code==200){
						uni.setStorageSync('userName',this.formData.userName)
						let data1=await queryByName(this.formData.userName)
						//console.log(data1);
						//console.log(data1.data[0].avatar);
						uni.setStorageSync('avatar',data1.data[0].avatar)
						uni.showToast({
							title:'登录成功',
							icon:'success',
							duration:2000
						})
						setTimeout(()=>{
							uni.switchTab({
								url:'/pages/index/index'
							})
						},2100)
					}
				}).catch(err=>console.log(err))
			}
		}
	}
</script>

<style>
	.content{
		align-items: center;
		width: 100%;
	}
	
	
</style>
